package com.dsteiner.widgets.client.domain;

import java.io.Serializable;
import java.util.Date;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.dsteiner.widgets.client.ObjectUtil;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Searchable
public class Distinct extends ModelObject{

	private static final long serialVersionUID = 6686961552160206434L;
	
	@Persistent
    private String className;
	
	@Persistent
	private Integer count = 1;
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
    @SearchableId
    private String id;
	
	@Persistent
	private String property;
	
	@Persistent
	private Date registeredDate = new Date();

	@Persistent
	private String value;
	

    public Distinct() {
	}

    public Distinct(String className, String property) {
		this(className, property, null);
	}

	public Distinct(String className, String property, Object value) {
		this(className, property, value, 1);
	}

	public Distinct(String className, String property, Object value, Integer count) {
		this.className = className;
		this.property = property;
		this.value = value != null ? value.toString() : null;
		this.count = count;
	}

	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (!(obj instanceof Distinct))
			return false;
		Distinct other = (Distinct) obj;
		return ObjectUtil.equals(getClassName(), other.getClassName()) 
		&& ObjectUtil.equals(getProperty(), other.getProperty())
		&& ObjectUtil.equals(getCount(), other.getCount())
		&& ObjectUtil.equals(getValue(), other.getValue());
	}

	@Override
	public int hashCode() {
		return className.hashCode() + property.hashCode() + (count != null ? count.hashCode() : 0) + 
		(value != null ? value.hashCode() : 0);
	}
	
	public String getClassName() {
		return className;
	}

	public Integer getCount() {
		return count;
	}

	@Override
	public String getId() {
		return id;
	}

	public String getProperty() {
		return property;
	}
	@Override
	public Date getRegisteredDate() {
		return registeredDate;
	}
	public String getValue() {
		return value;
	}
	public void increaseCount() {
		count++;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	@Override
	public void setId(String id) {
		this.id = id;
	}
	public void setProperty(String property) {
		this.property = property;
	}

	public void setValue(Object value) {
		this.value = value != null ? value.toString() : null;
	}
    @Override
    public String toString() {
    	return className+"|"+property+"|"+value+"|"+count;
    }

	public void decreaseCount() {
		count--;
	}
}